package receive;

import com.alibaba.fastjson.JSONObject;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class Servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();
        String question = "", questionName = "";
        Map<String, Object> map = new HashMap();
//        String companyname="companyname",businesssector="businesssector",
//                employeenum="employeenum",companylocation="companylocation",
//                contactname="contactname",contactjobtitle="contactjobtitle",
//                contactemail="contactemail",contacttelephone="contacttelephone";
        map.put("companyname",req.getParameter("companyname"));
        map.put("businesssector",req.getParameter("businesssector"));
        map.put("employeenum",req.getParameter("employeenum"));
        map.put("companylocation",req.getParameter("companylocation"));
        map.put("contactname",req.getParameter("contactname"));
        map.put("contactjobtitle",req.getParameter("contactjobtitle"));
        map.put("contactemail",req.getParameter("contactemail"));
        map.put("contacttelephone",req.getParameter("contacttelephone"));





        int currentMark = 0, totalMark = 0, averageMark = 0;
        for (int i = 1; i <= 48; i++) {
            question = "q" + i;
//            String mark = req.getParameter(question);
//            totalMark += Integer.parseInt(mark);
            currentMark = Integer.parseInt(req.getParameter(question));
            totalMark += currentMark;
            if (i == 4) {
                questionName = "Legal Compliance";
                averageMark = Math.round(totalMark / 4);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 8) {
                questionName = "Hours of Work and Leave";
                averageMark = Math.round(totalMark / 4);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 13) {
                questionName = "Policies & Practices";
                averageMark = Math.round(totalMark / 5);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 16) {
                questionName = "Employee Relations";
                averageMark = Math.round(totalMark / 3);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 22) {
                questionName = "Performance Management";
                averageMark = Math.round(totalMark / 6);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 24) {
                questionName = "Training and Development";
                averageMark = Math.round(totalMark / 2);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 29) {
                questionName = "Recruitment & Retention";
                averageMark = Math.round(totalMark / 5);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 31) {
                questionName = "Change and Reorganisation";
                averageMark = Math.round(totalMark / 2);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 36) {
                questionName = "Pay and Benefits";
                averageMark = Math.round(totalMark / 5);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 39) {
                questionName = "Employee Communication & Engagement";
                averageMark = Math.round(totalMark / 3);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 42) {
                questionName = "Health and Safety";
                averageMark = Math.round(totalMark / 3);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 45) {
                questionName = "Covid Related";
                averageMark = Math.round(totalMark / 3);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
            if (i == 48) {
                questionName = "Employee Records and GDPR";
                averageMark = Math.round(totalMark / 3);
                map.put(questionName, averageMark);
                totalMark = 0;
                averageMark = 0;
            }
        }

        String result = JSONObject.toJSONString(map);
        context.setAttribute("result",result);
        System.out.println(result);
        resp.sendRedirect("http://localhost:8080/CS335Lab/html/download.html");
    }
}
